package com.xuyisen.web;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xuyisen.bean.User;
import com.xuyisen.domain.Criteria;
import com.xuyisen.domain.Restrictions;
import com.xuyisen.service.UserService;

/**
 * 用户控制类
 * 
 * @author sen
 *
 */
@RestController
@RequestMapping("users")
public class UserController {
	@Autowired
	private UserService userService;
	HashMap<String, Object> hashMap = new HashMap<>();
	// 用户登录
	@RequestMapping("login")
	public HashMap<String, Object> login(User user, HttpSession httpSession) {
		System.out.println(user.getNum());
		System.out.println(user.getPassword());
		User user2 = userService.findByNum(user.getNum());
		if (user2 != null) {
			if (user2.getPassword().equals(user.getPassword())) {
				httpSession.setAttribute("LOGIN_INFO", user2);
				httpSession.setAttribute("user_id", user2.getId());
				hashMap.put("status", "success");
				hashMap.put("msg", "登录成功");
			} else {
				hashMap.put("status", "failure");
				hashMap.put("msg", "登录失败！密码错误，请重新输入");
			}
		} else {
			hashMap.put("status", "failure");
			hashMap.put("msg", "登录失败！用户名不存在，请重新输入！");
		}
		return hashMap;
	}

	// 分页排序
	@RequestMapping("find")
	public HashMap<String, Object> find(@RequestParam(value = "fieldName") String fieldName,
			@RequestParam(value = "value") String value, @RequestParam(value = "page") Integer page,
			@RequestParam(value = "pageSize") Integer pageSize, @RequestParam(value = "field") String field,
			@RequestParam(value = "direction") String direction) {
		// 判断排序方式
		Pageable pageable;
		Sort sort;
		if (direction.equals("asc")) {
			sort = new Sort(Sort.Direction.ASC, field);
		} else if (direction.equals("desc")) {
			sort = new Sort(Sort.Direction.DESC, field);
		} else {
			sort = new Sort(Sort.Direction.DESC, "id");
		}

		if (value != null && value != "") {
			pageable = new PageRequest(0, pageSize, sort);
			Criteria<User> criteria = new Criteria<>();
			criteria.add(Restrictions.eq(fieldName, value, true));
			Page<User> user = userService.find(criteria, pageable);
			hashMap.put("status", "success");
			hashMap.put("msg", user);

			return hashMap;
		} else {
			throw new NullPointerException();
//			pageable = new PageRequest(page, pageSize, sort);
//			Page<User> user = userService.findAll(pageable);
//			hashMap.put("status", "success");
//			hashMap.put("msg", user);
//			return hashMap;

		}

	}

	// 根据ID进行删除
	@RequestMapping("deleteUser")
	public HashMap<String, Object> deleteUser(@RequestBody Integer id) {
		// 调用方法进行删除
		userService.delete(id);
		// 利用上一个方法重新获取数据
		hashMap.put("status", "success");
		return hashMap;

	}

	// 大量删除
	@RequestMapping("deleteUsers")
	public HashMap<String, Object> deleteUsers(@RequestBody List<Integer> ids) {
		// 调用方法进行删除
		for (Integer id : ids) {
			userService.delete(id);
		}
		hashMap.put("status", "success");
		return hashMap;
		// 利用上一个方法重新获取数据
	}

	// 返回头部信息
	@RequestMapping("header")
	public HashMap<String, Object> header(HttpSession httpSession) {
		if (httpSession.getAttribute("LOGIN_INFO") == null) {
			hashMap.put("status", "failure");
			hashMap.put("msg", "not_login");
		} else {
			hashMap.put("status", "success");
			hashMap.put("msg", httpSession.getAttribute("LOGIN_INFO"));
		}
		return hashMap;
	}

	// 根据ID进行查找
	@RequestMapping("findOne")
	public HashMap<String, Object> findOne(@RequestParam(value = "id") Integer id,
			HttpSession httpSession) {
		Object obj = httpSession.getAttribute("user_id");
		if (obj != null) {
			User user;
			int id1 = (int) obj;
			if (id==null) {
				user = userService.findOne(id1);
			}else{
				user=userService.findOne(id);
			}
			hashMap.put("status", "success");
			hashMap.put("msg", user);		
		}else{
			hashMap.put("status", "failure");
			hashMap.put("msg", "not_login");
		}
		return hashMap;
	}
	// 保存数据
	@RequestMapping("update")
	public HashMap<String, Object> save(User user) {
		userService.save(user);
		hashMap.put("status", "success");
		return hashMap;
	}

}
